package cz.cuni.mff.ufal.volk.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

import cz.cuni.mff.ufal.volk.Speech;
import cz.cuni.mff.ufal.volk.Text;
import cz.cuni.mff.ufal.volk.UnsupportedLanguageException;

public class MultiTextToSpeech implements TextToSpeech {

	public MultiTextToSpeech(Collection<TextToSpeech> ttsCollection) {
		ttsCollection.getClass();
		underlyingTts = new ArrayList<TextToSpeech>(ttsCollection);
	}

	private final List<TextToSpeech> underlyingTts;

	@Override
	public boolean languageSupported(String language) {
		for (TextToSpeech tts : underlyingTts)
			if (tts.languageSupported(language)) return true;
		return false;
	}

	@Override
	public List<String> supportedLanguages() {
		TreeSet<String> languages = new TreeSet<String>();
		for (TextToSpeech tts : underlyingTts)
			languages.addAll(tts.supportedLanguages());
		return new ArrayList<String>(languages);
	}

	@Override
	public Speech process(Text text, String voice) {
		String language = text.getLanguage();
		for (TextToSpeech tts : underlyingTts) {
			if (tts.languageSupported(language))
				return tts.process(text, voice);
		}

		throw new UnsupportedLanguageException(language);
	}

	@Override
	public List<String> availableVoices(String language) {
		TreeSet<String> voices = new TreeSet<String>();
		for (TextToSpeech tts : underlyingTts)
			voices.addAll(tts.availableVoices(language));

		return new ArrayList<String>(voices);
	}

}
